package itheima.zuoye.Test4;

import java.util.Scanner;

 import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        // 1. 定义学生类 Student
        Scanner scanner = new Scanner(System.in);

        // 2. 键盘录入 3 个学生信息
        List<Student> students = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入第 " + (i + 1) + " 个学生的姓名和年龄（格式：姓名 年龄）:");
            String input = scanner.nextLine();
            String[] parts = input.split(" ");
            String name = parts[0];
            int age = Integer.parseInt(parts[1]);
            students.add(new Student(name, age));
        }

        // 3. 键盘录入 3 个居住地信息
        List<String> addresses = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入第 " + (i + 1) + " 个居住地:");
            String address = scanner.nextLine();
            addresses.add(address);
        }

        // 4. 将两个 list 集合中的数据收集到同一个 map 集合中
        Map<Student, String> studentAddressMap = new LinkedHashMap<>();
        for (int i = 0; i < students.size(); i++) {
            studentAddressMap.put(students.get(i), addresses.get(i));
        }

        // 5. 要求 map 集合中不能存在相同的学生信息. 并按照学生年龄降序排列
        studentAddressMap = studentAddressMap.entrySet().stream()
                .sorted(Map.Entry.<Student, String>comparingByKey(Comparator.comparing(Student::getAge).reversed()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (existing, replacement) -> existing,
                        LinkedHashMap::new



                ));

        // 6. 使用 Stream 流输出集合中所有姓张的人信息
        studentAddressMap.entrySet().stream()
                .filter(entry -> entry.getKey().getName().startsWith("张"))
                .forEach(entry -> System.out.println("姓名: " + entry.getKey().getName() + ", 年龄: " + entry.getKey().getAge() + ", 地址: " + entry.getValue()));

    }


}
